﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
using System.Data;

namespace weiqijiance.温湿度大气压
{
    public class wsddqybass
    {
        public float m_fWenDu = 0;
        public float m_fShiDu = 0;
        public float m_fYaLi = 0;

        public long wdjz = 0;
        public long sdjz = 0;
        public long yljz = 0;

        public mainform m_mainform;
        public SerialPort commex = new SerialPort();//废气仪串口
        public virtual bool openCOM(string sSet1)
        {
            string[] szbzlCOM = new string[5];
            if (sSet1.Length > 0)
                szbzlCOM = sSet1.Split(':');
            //打开串口
            commex.PortName = szbzlCOM[0];
            commex.BaudRate = Convert.ToInt32(szbzlCOM[1]);

            commex.NewLine = "/r/n";
            commex.RtsEnable = true;//根据实际情况吧。 
            commex.ReadTimeout = 300;//读取数据延时，避免读取不全数据

            if (szbzlCOM[3] == "0")
                commex.Parity = Parity.None;
            else
                commex.Parity = Parity.Even;
            // ////添加事件注册  
            //  comm.DataReceived += comm_Data;
            try
            {
                commex.Open();
            }
            catch (Exception )
            {
              //lzj2020  MessageBox.Show(string.Format("{0}\n请检查电脑的串口故障", ex.Message));
                return false;
            }
            return true;
        }

        /// <summary>
        /// 发送开始测量
        /// </summary>
        /// <returns></returns>
        public virtual bool SendDeviceCmd()
        {
            string sql;
            sql = string.Format(@"Select *  from HJXX where DETECT_ID=1 ");
            DataTable dt = m_mainform.objbass.objdb.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    float.TryParse(dt.Rows[i]["WenDu"].ToString().Trim(), out m_fWenDu);
                    float.TryParse(dt.Rows[i]["ShiDu"].ToString().Trim(), out m_fShiDu);
                    float.TryParse(dt.Rows[i]["YaLi"].ToString().Trim(), out m_fYaLi);

                }
            }
            return true;
        }

        public virtual bool SendDevicebd(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6)
        {
            return true;
        }
        public virtual bool setjianding(float wd, float sd, float dqy)
        {
            return true;
        }

        /// 求一个16位数的补码
        /// </summary>
        /// <param name="OriginalCode">传入一个Int32整型</param>
        /// <returns></returns>
        public static long ConvertComplementCode(long OriginalCode)
        {

            long a = Int16.MaxValue;
            long b = Int16.MinValue;
            long c = a - b;
            long d = 0;
            if (OriginalCode <= Int16.MaxValue && OriginalCode >= 0)
            {
                d = OriginalCode;
            }
            else if (OriginalCode > 0)
            {
                d = -(c - OriginalCode + 1);
            }
            else
            {
                d = c + OriginalCode + 1;
            }
            return d;
        }
    }
}
